%% This program plots the reversals and omegas data;


%Don't forget to change the name of the folders to load acordingly to the
%type of experiments being analyzed; don't forget to change the title;
%do this for each graph

%% Load all datasets of the experiment %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all

load ('/Users/anapereira/Dropbox/Matlab/data/revandomegasarray.mat');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp30_ZC2834_F1/RRF3_exp30_ZC2834_OP choice OP/reversals and omegas/RRF3_exp30_ZC2834_OP_to_OP_reversals_F1');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp30_ZC2834_F1/RRF3_exp30_ZC2834_OP choice PA/reversals and omegas/RRF3_exp30_ZC2834_OP_to_PA_reversals_F1');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp30_ZC2834_F1/RRF3_exp30_ZC2834_PA choice OP/reversals and omegas/RRF3_exp30_ZC2834_PA_to_OP_reversals_F1');
load ('/Users/anapereira/Dropbox/Matlab/data/matlab data/RRF3_exp30_ZC2834_F1/RRF3_exp30_ZC2834_PA choice PA/reversals and omegas/RRF3_exp30_ZC2834_PA_to_PA_reversals_F1');

dataOPOP = RRF3_Exp30_ZC2834_OP_to_OP_reversals_F1; %change name
dataOPPA = RRF3_Exp30_ZC2834_OP_to_PA_reversals_F1; %change name
dataPAOP = RRF3_Exp30_ZC2834_PA_to_OP_reversals_F1; %change name
dataPAPA = RRF3_Exp30_ZC2834_PA_to_PA_reversals_F1; %change name

spacedistr = dataOPOP {2,28,1}; %gives the array in which the distance to stimuli is divided
revandomeganalyzis {2,26,1} = spacedistr;

%% Analyzis for dataset OP to OP 

%**********************REVERSALS PER SECOND**********************
sujeitosOPOP = size (dataOPOP,1)-1;% because it is counting with the label row
sujeitosloopOPOP = sujeitosOPOP + 1;%because the 1st line is always the title

averagerevpersec = dataOPOP (2:sujeitosloopOPOP, 15); %extract average number of reversals a given 
%worm does in one second

averagerevpersecvalues = zeros(sujeitosOPOP,1);
for i=1:sujeitosOPOP;
    rpsvalues = averagerevpersec {i,1};
    averagerevpersecvalues(i,1) = rpsvalues;
end

if sujeitosOPOP >= 3 % each individual comes with one value for reversals
[H, p, W] = swtest(averagerevpersecvalues, 0.05);%null hypothesis data comes from normal distribuition; if h=1 reject null hypothesis that data comes from a normal distribuition
elseif sujeitosOPOP ==2 
    p = NaN;
elseif sujeitosOPOP ==1 
    p = NaN;
elseif sujeitosOPOP ==0
    p= NaN;
end
    
averagerevpersecall = mean (averagerevpersecvalues); %gives the avr number of reversals per second of OP to OP worms
desvpadraoavrrevpersecall = std (averagerevpersecvalues);
semavrrevpersecall = desvpadraoavrrevpersecall./ (sqrt (sujeitosOPOP));
medianaavrrevpersecall = median (averagerevpersecvalues);

revandomeganalyzis {2,2,1} = averagerevpersecvalues;
revandomeganalyzis {3,2,1} = p;
revandomeganalyzis {4,2,1} = averagerevpersecall;
revandomeganalyzis {5,2,1} = desvpadraoavrrevpersecall;
revandomeganalyzis {6,2,1} = sujeitosOPOP;
revandomeganalyzis {7,2,1} = semavrrevpersecall;
revandomeganalyzis {8,2,1} = medianaavrrevpersecall;

%% *******PROPORTION OF WORMS THAT MAKE REVERSALS************

% find how may worms make reversals in a given group 
wormsthatrevers = [];

for n = 2:sujeitosloopOPOP;
    wormrevcount = dataOPOP {n,11,1};
    %wormrevcountvalues = wormrevcount {1,1};
    yesornorev = size (wormrevcount, 1);
    if yesornorev ~= 0
       wormsthatrevers = [ wormsthatrevers, 1];
    elseif yesornorev == 0
       wormsthatrevers = [ wormsthatrevers, 0];
    end
end

nwormsthatrevers = sum (wormsthatrevers); %extracts from all worms of the dataset of many of them show reversals
propwormsthatrevers = nwormsthatrevers./sujeitosOPOP;%proportion of worms in the data set that revers

%% ******************REVERSAL DURATION*****************************
%given the reversals done by all worms, what is the average duration of them %%%%%%%%%%%%%%%%%%%%%%%
indrevduration = dataOPOP (2:sujeitosloopOPOP, 11); %extracts duration of reversals for each animal
indrevdurationvalues = [];

if nwormsthatrevers ==0;
    indrevdurationvalues = zeros (nwormsthatrevers, 1);
elseif nwormsthatrevers ~=0;
   for j=1:sujeitosOPOP;
        irdvalues = indrevduration {j,1};
        indrevdurationvalues = [indrevdurationvalues; irdvalues];
   end
end

totalrevOPOP = size (indrevdurationvalues, 1);

if totalrevOPOP >= 3
[H, p, W] = swtest(indrevdurationvalues, 0.05);    
elseif totalrevOPOP ==2 
    p = NaN;
elseif totalrevOPOP ==1 
    p = NaN;
elseif totalrevOPOP ==0
    p= NaN;
end
 
revdurall = mean (indrevdurationvalues);

if isnan (revdurall) == true
    indrevdurationvaluesfinal = [];
    revdurallfinal = 0;
elseif isnan (revdurall) == false
    indrevdurationvaluesfinal = indrevdurationvalues;
    revdurallfinal = revdurall;
end

desvpadraorevdurall = std (indrevdurationvaluesfinal);
semrevdurall = desvpadraorevdurall./ (sqrt (totalrevOPOP));
medianarevdurall = median (indrevdurationvaluesfinal);

revandomeganalyzis {2,6,1} = indrevdurationvaluesfinal;
revandomeganalyzis {3,6,1} = p;
revandomeganalyzis {4,6,1} = revdurallfinal;
revandomeganalyzis {5,6,1} = desvpadraorevdurall;
revandomeganalyzis {6,6,1} = totalrevOPOP;
revandomeganalyzis {7,6,1} = semrevdurall;
revandomeganalyzis {8,6,1} = medianarevdurall;
revandomeganalyzis {9,6,1} = propwormsthatrevers;

%% ************* REVERSAL DISTRIBUITION- at which distance the worms make
%%more reversals **************

revdist = dataOPOP (2:sujeitosloopOPOP, 25); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes reversals
%zerosvector = zeros (1,11);

revdistvalues = [];
for i=1:sujeitosOPOP;
    irdvalues = revdist {i,1};%individual reversal distribuition values
    revdistvalues = [revdistvalues; irdvalues];% to create a vector with the distribuition    
end
 
distribuicoesrevOPOP = size (revdistvalues, 1);%another way to count subjects

if distribuicoesrevOPOP > 1 % when there is more than one subject in the analyzis
    revdistall = mean (revdistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraorevdistall = std (revdistvalues);
    semrevdistall = desvpadraorevdistall./ (sqrt (distribuicoesrevOPOP));%in this case, what matters is the number of individuals that did the reversals
    medianarevdistall = median (revdistvalues);    
elseif distribuicoesrevOPOP == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    revdistall = revdistvalues;
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = revdistvalues;
elseif distribuicoesrevOPOP == 0 %if no worm makes reversals or omegas
    revdistall = zeros (1,10);
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = zeros (1,10);
end

revandomeganalyzis {2,10,1} = revdistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,10,1} = revdistall;%gives the average number of reversals at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,10,1} = desvpadraorevdistall;
revandomeganalyzis {6,10,1} = distribuicoesrevOPOP;
revandomeganalyzis {7,10,1} = semrevdistall;
revandomeganalyzis {8,10,1} = medianarevdistall;

%% ***** PROPORTION OF WORMS THAT REVERS AT A GIVEN POINT IN SPACE **********

wormrevspacedist = zeros (1,10);
wormrevspacedistdivision = [];
for k=1:10
    for l = 1:sujeitosOPOP
        if revdistvalues (l,k,1) ==0 %extracts weather there are reversals or not (doesn't matter how many) but only from one worm
           wormrevspacedistdivision = [wormrevspacedistdivision;0];
        elseif revdistvalues (l,k,1) ~=0
           wormrevspacedistdivision = [wormrevspacedistdivision;1]; 
        end
    end
        wormsrevsum = sum (wormrevspacedistdivision);
        wormrevspacedist (1,k,1) = wormsrevsum;
        wormrevspacedistdivision = [];  
end

if sujeitosOPOP == 1 
    proportionofworms =  wormrevspacedist;
elseif sujeitosOPOP ~= 1
    proportionofworms =  wormrevspacedist./sujeitosOPOP;
end

revandomeganalyzis {9,10,1} = proportionofworms;
    
%% *********** OMEGAS PER SECOND ***********************
averageomepersec = dataOPOP (2:sujeitosloopOPOP, 23); %extract the avr nume of omegas each worm does per second 

averageomepersecvalues = zeros(sujeitosOPOP,1);
for i=1:sujeitosOPOP;
    opsvalues = averageomepersec {i,1};
    averageomepersecvalues(i,1) = opsvalues;
end

if sujeitosOPOP>= 3
[H, p, W] = swtest(averageomepersecvalues, 0.05);%null hypothesis data comes from normal distribuition; if h=1 reject null hypothesis that data comes from a normal distribuition
elseif sujeitosOPOP ==2 
    p = NaN;
elseif sujeitosOPOP ==1 
    p = NaN;
elseif sujeitosOPOP ==0
    p= NaN;
end

averageomepersecall = mean (averageomepersecvalues);
desvpadraoavromepersecall = std (averageomepersecvalues);
semavromepersecall = desvpadraoavromepersecall./ (sqrt (sujeitosOPOP));
medianaavromepersecall = median (averageomepersecvalues);

revandomeganalyzis {2,14,1} = averageomepersecvalues;
revandomeganalyzis {3,14,1} = p;
revandomeganalyzis {4,14,1} = averageomepersecall;
revandomeganalyzis {5,14,1} = desvpadraoavromepersecall;
revandomeganalyzis {6,14,1} = sujeitosOPOP;
revandomeganalyzis {7,14,1} = semavromepersecall;
revandomeganalyzis {8,14,1} = medianaavromepersecall;


%% **********PROPORTION OF WORMS THAT MAKE OMEGAS *******************************
wormsthatomeg = []; % off all worms, how many worms perform at least one omega turn at a given point in time

for n = 2:sujeitosloopOPOP;
    wormomecount = dataOPOP (n,19,1);
    wormomecountvalues = wormomecount {1,1};
    yesornoome = size (wormomecountvalues);
    if yesornoome ~= 0
       wormsthatomeg = [ wormsthatomeg, 1];
    elseif yesornoome == 0
       wormsthatomeg = [ wormsthatomeg, 0];
    end
end


nwormsthatomeg = sum (wormsthatomeg); %extracts from all worms of the dataset of many of them show omegas at any time point
propwormsthatomeg = nwormsthatomeg./sujeitosOPOP;%proportion of worms in the data set that do omegas

%% *********************REVERSAL DURATION************************
indomeduration = dataOPOP (2:sujeitosloopOPOP, 19); %extracts duration of reversals for each animal
indomedurationvalues = [];

if nwormsthatomeg ==0;
    indomedurationvalues = zeros (nwormsthatomeg, 1);
elseif nwormsthatomeg ~=0;
   for j=1:sujeitosOPOP;
        iodvalues = indomeduration {j,1};
        indomedurationvalues = [indomedurationvalues; iodvalues];
   end
end

totalomeOPOP = size (indomedurationvalues, 1);

if totalomeOPOP>= 3
[H, p, W] = swtest(indomedurationvalues, 0.05);
elseif totalomeOPOP ==2 
    p = NaN;
elseif totalomeOPOP ==1 
    p = NaN;
elseif totalomeOPOP ==0
    p= NaN;
end

omedurall = mean (indomedurationvalues);

if isnan (omedurall) == true
    indomedurationvaluesfinal = [];
    omedurallfinal = 0;
elseif isnan (omedurall) == false
    indomedurationvaluesfinal = indomedurationvalues;
    omedurallfinal = omedurall;
end

desvpadraoomedurall = std (indomedurationvaluesfinal);
semomedurall = desvpadraoomedurall./ (sqrt (totalomeOPOP));
medianaomedurall = median (indomedurationvaluesfinal);

revandomeganalyzis {2,18,1} = indomedurationvaluesfinal;
revandomeganalyzis {3,18,1} = p;
revandomeganalyzis {4,18,1} = omedurallfinal;
revandomeganalyzis {5,18,1} = desvpadraoomedurall;
revandomeganalyzis {6,18,1} = totalomeOPOP;
revandomeganalyzis {7,18,1} = semomedurall;
revandomeganalyzis {8,18,1} = medianaomedurall;
revandomeganalyzis {9,18,1} = propwormsthatomeg;

%% ******************* OMEGA DISTRIBUITION ***************
% at which distance from the stimulus the worms make omegas

omedist = dataOPOP (2:sujeitosloopOPOP, 27); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes omegas

omedistvalues = [];
for i=1:sujeitosOPOP;
    iodvalues = omedist {i,1};%individual reversal distribuition values
    omedistvalues = [omedistvalues; iodvalues];    
end
 
distribuicoesomeOPOP = size (omedistvalues, 1);

if distribuicoesomeOPOP > 1 % when there is more than one subject in the group
    omedistall = mean (omedistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraoomedistall = std (omedistvalues);
    semomedistall = desvpadraoomedistall./ (sqrt (distribuicoesomeOPOP));%in this case, what matters is the number of individuals that did the reversals
    medianaomedistall = median (omedistvalues);    
elseif  distribuicoesomeOPOP == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    omedistall = omedistvalues;
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = omedistvalues;
elseif distribuicoesomeOPOP == 0 %if any worm makes reversals or omegas
    omedistall = zeros (1,10);
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = zeros (1,10);
end

revandomeganalyzis {2,22,1} = omedistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,22,1} = omedistall;%gives the average number of omegas at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,22,1} = desvpadraoomedistall;
revandomeganalyzis {6,22,1} = distribuicoesomeOPOP;
revandomeganalyzis {7,22,1} = semomedistall;
revandomeganalyzis {8,22,1} = medianaomedistall;

%% **************PROPORTION OF WORMS THAT OMEGA AT A GIVEN POINT IN SPACE***************************** 
wormomespacedist = zeros (1,10);
wormomespacedistdivision = [];
for k=1:10
    for l = 1:sujeitosOPOP
        if omedistvalues (l,k,1) ==0
           wormomespacedistdivision = [wormomespacedistdivision;0];
        elseif omedistvalues (l,k,1) ~=0
           wormomespacedistdivision = [wormomespacedistdivision;1]; 
        end
    end
        wormomesum = sum (wormomespacedistdivision);
        wormomespacedist (1,k,1) = wormomesum;
        wormomespacedistdivision = [];  
end

if sujeitosOPOP == 1 
    proportionofwormsome = wormomespacedist;
elseif sujeitosOPOP ~= 1
    proportionofwormsome =  wormomespacedist./sujeitosOPOP;
end

revandomeganalyzis {9,22,1} = proportionofwormsome;


%% ********************ANALYZE FOR DATASET OP to PA ******************************* 

% ********************************* REVERSALS PER SECOND ****************
sujeitosOPPA = size (dataOPPA,1)-1;% because it is counting with the label row
sujeitosloopOPPA = sujeitosOPPA + 1;%because the 1st line is always the title

averagerevpersec = dataOPPA (2:sujeitosloopOPPA, 15); %extract average number of reversals a given 
%worm does in one second

averagerevpersecvalues = zeros(sujeitosOPPA,1);
for i=1:sujeitosOPPA;
    rpsvalues = averagerevpersec {i,1};
    averagerevpersecvalues(i,1) = rpsvalues;
end
 
if sujeitosOPPA >= 3
[H, p, W] = swtest(averagerevpersecvalues, 0.05);
elseif sujeitosOPPA ==2 
    p = NaN;
elseif sujeitosOPPA ==1 
    p = NaN;
elseif sujeitosOPPA ==0
    p= NaN;
end

averagerevpersecall = mean (averagerevpersecvalues); %gives the avr number of reversals per second of OP to OP worms
desvpadraoavrrevpersecall = std (averagerevpersecvalues);
semavrrevpersecall = desvpadraoavrrevpersecall./ (sqrt (sujeitosOPPA));
medianaavrrevpersecall = median (averagerevpersecvalues);

revandomeganalyzis {2,3,1} = averagerevpersecvalues;
revandomeganalyzis {3,3,1} = p;
revandomeganalyzis {4,3,1} = averagerevpersecall;
revandomeganalyzis {5,3,1} = desvpadraoavrrevpersecall;
revandomeganalyzis {6,3,1} = sujeitosOPPA;
revandomeganalyzis {7,3,1} = semavrrevpersecall;
revandomeganalyzis {8,3,1} = medianaavrrevpersecall;

%% *****************PROPORTION OF WORMS THAT REVERSE ******************
wormsthatrevers = [];

for n = 2:sujeitosloopOPPA;
    wormrevcount = dataOPPA (n,11,1);
    wormrevcountvalues = wormrevcount {1,1};
    yesornorev = size (wormrevcountvalues);
    if yesornorev ~= 0
       wormsthatrevers = [ wormsthatrevers, 1];
    elseif yesornorev == 0
       wormsthatrevers = [ wormsthatrevers, 0];
    end
end

nwormsthatrevers = sum (wormsthatrevers); %extracts from all worms of the dataset of many of them show reversals
propwormsthatrevers = nwormsthatrevers./sujeitosOPPA;%proportion of worms in the data set that revers

%% *****************REVERSALS DURATION************************
indrevduration = dataOPPA (2:sujeitosloopOPPA, 11); %extracts duration of reversals for each animal
indrevdurationvalues = [];

if nwormsthatrevers ==0;
    indrevdurationvalues = zeros (nwormsthatrevers, 1);
elseif nwormsthatrevers ~=0;
   for j=1:sujeitosOPPA;
        irdvalues = indrevduration {j,1};
        indrevdurationvalues = [indrevdurationvalues; irdvalues];
   end
end

totalrevOPPA = size (indrevdurationvalues, 1);

if totalrevOPPA>=3
[H, p, W] = swtest(indrevdurationvalues, 0.05);
elseif totalrevOPPA ==2 
    p = NaN;
elseif totalrevOPPA ==1 
    p = NaN;
elseif totalrevOPPA ==0
    p= NaN;
end

revdurall = mean (indrevdurationvalues);

if isnan (revdurall) == true
    indrevdurationvaluesfinal = [];
    revdurallfinal = 0;
elseif isnan (revdurall) == false
    indrevdurationvaluesfinal = indrevdurationvalues;
    revdurallfinal = revdurall;
end

desvpadraorevdurall = std (indrevdurationvaluesfinal);
semrevdurall = desvpadraorevdurall./ (sqrt (totalrevOPPA));
medianarevdurall = median (indrevdurationvaluesfinal);

revandomeganalyzis {2,7,1} = indrevdurationvaluesfinal;
revandomeganalyzis {3,7,1} = p;
revandomeganalyzis {4,7,1} = revdurallfinal;
revandomeganalyzis {5,7,1} = desvpadraorevdurall;
revandomeganalyzis {6,7,1} = totalrevOPPA;
revandomeganalyzis {7,7,1} = semrevdurall;
revandomeganalyzis {8,7,1} = medianarevdurall;
revandomeganalyzis {9,7,1} = propwormsthatrevers;

%% ******************** REVERSAL DISTRIBUITION ************************************

revdist = dataOPPA (2:sujeitosloopOPPA, 25); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes reversals

revdistvalues = [];
for i=1:sujeitosOPPA;
    irdvalues = revdist {i,1};%individual reversal distribuition values
    revdistvalues = [revdistvalues; irdvalues];    
end
 
distribuicoesrevOPPA = size (revdistvalues, 1);

if distribuicoesrevOPPA > 1 % when there is more than one subject with reversals
    revdistall = mean (revdistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraorevdistall = std (revdistvalues);
    semrevdistall = desvpadraorevdistall./ (sqrt (distribuicoesrevOPPA));%in this case, what matters is the number of individuals that did the reversals
    medianarevdistall = median (revdistvalues);    
elseif distribuicoesrevOPPA == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    revdistall = revdistvalues;
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = revdistvalues;
elseif distribuicoesrevOPPA == 0 %if any worm makes reversals or omegas
    revdistall = zeros (1,10);
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = zeros (1,10);
end

revandomeganalyzis {2,11,1} = revdistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,11,1} = revdistall;%gives the average number of reversals at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,11,1} = desvpadraorevdistall;
revandomeganalyzis {6,11,1} = distribuicoesrevOPPA;
revandomeganalyzis {7,11,1} = semrevdistall;
revandomeganalyzis {8,11,1} = medianarevdistall;

%% ****************PROPORTION OF WORMS THAT REVERSE AT A GIVEN POINT IN SPACE ************

wormrevspacedist = zeros (1,10);
wormrevspacedistdivision = [];
for k=1:10
    for l = 1:sujeitosOPPA
        if revdistvalues (l,k,1) ==0
           wormrevspacedistdivision = [wormrevspacedistdivision;0];
        elseif revdistvalues (l,k,1) ~=0
           wormrevspacedistdivision = [wormrevspacedistdivision;1]; 
        end
    end
        wormsrevsum = sum (wormrevspacedistdivision);
        wormrevspacedist (1,k,1) = wormsrevsum;
        wormrevspacedistdivision = [];  
end

if sujeitosOPPA == 1 
    proportionofworms =  wormrevspacedist;
elseif sujeitosOPPA ~= 1
    proportionofworms =  wormrevspacedist./sujeitosOPPA;%proportion of worms that make reversals at a given point
end

revandomeganalyzis {9,11,1} = proportionofworms;
 
%% *********************OMEGAS PER SECOND**********************************
averageomepersec = dataOPPA (2:sujeitosloopOPPA, 23); %extract the avr nume of omegas each worm does per second 

averageomepersecvalues = zeros(sujeitosOPPA,1);
for i=1:sujeitosOPPA;
    opsvalues = averageomepersec {i,1};
    averageomepersecvalues(i,1) = opsvalues;
end

if sujeitosOPPA>=3
[H, p, W] = swtest(averageomepersecvalues, 0.05);   
elseif sujeitosOPPA ==2
    p=NaN;
elseif sujeitosOPPA ==1 
    p = NaN;
elseif sujeitosOPPA ==0
    p= NaN;
end

averageomepersecall = mean (averageomepersecvalues);
desvpadraoavromepersecall = std (averageomepersecvalues);
semavromepersecall = desvpadraoavromepersecall./ (sqrt (sujeitosOPPA));
medianaavromepersecall = median (averageomepersecvalues);

revandomeganalyzis {2,15,1} = averageomepersecvalues;
revandomeganalyzis {3,15,1} = p;
revandomeganalyzis {4,15,1} = averageomepersecall;
revandomeganalyzis {5,15,1} = desvpadraoavromepersecall;
revandomeganalyzis {6,15,1} = sujeitosOPPA;
revandomeganalyzis {7,15,1} = semavromepersecall;
revandomeganalyzis {8,15,1} = medianaavromepersecall;

%% ******************** PROPORTION OF WORMS THAT MAKE OMEGAS ********
wormsthatomeg = []; % off all worms, how many worms perform at least one omega turn at a given point in time

for n = 2:sujeitosloopOPPA;
    wormomecount = dataOPPA (n,19,1);
    wormomecountvalues = wormomecount {1,1};
    yesornoome = size (wormomecountvalues);
    if yesornoome ~= 0
       wormsthatomeg = [ wormsthatomeg, 1];
    elseif yesornoome == 0
       wormsthatomeg = [ wormsthatomeg, 0];
    end
end


nwormsthatomeg = sum (wormsthatomeg); %extracts from all worms of the dataset of many of them show omegas at any time point
propwormsthatomeg = nwormsthatomeg./sujeitosOPPA;%proportion of worms in the data set that do omegas

%% ***************OMEGAS DURATION *********************
indomeduration = dataOPPA (2:sujeitosloopOPPA, 19); %extracts duration of reversals for each animal
indomedurationvalues = [];

if nwormsthatomeg ==0;
    indomedurationvalues = zeros (nwormsthatomeg, 1);
elseif nwormsthatomeg ~=0;
   for j=1:sujeitosOPPA;
        iodvalues = indomeduration {j,1};
        indomedurationvalues = [indomedurationvalues; iodvalues];
   end
end

totalomeOPPA = size (indomedurationvalues, 1);

if totalomeOPPA>= 3
[H, p, W] = swtest(indomedurationvalues, 0.05);    
elseif totalomeOPPA ==2 
    p = NaN;
elseif totalomeOPPA ==1 
    p = NaN;
elseif totalomeOPPA ==0
    p= NaN;
end

omedurall = mean (indomedurationvalues);

if isnan (omedurall) == true
    indomedurationvaluesfinal = [];
    omedurallfinal = 0;
elseif isnan (omedurall) == false
    indomedurationvaluesfinal = indomedurationvalues;
    omedurallfinal = omedurall;
end

desvpadraoomedurall = std (indomedurationvaluesfinal);
semomedurall = desvpadraoomedurall./ (sqrt (totalomeOPPA));
medianaomedurall = median (indomedurationvaluesfinal);

revandomeganalyzis {2,19,1} = indomedurationvaluesfinal;
revandomeganalyzis {3,19,1} = p;
revandomeganalyzis {4,19,1} = omedurallfinal;
revandomeganalyzis {5,19,1} = desvpadraoomedurall;
revandomeganalyzis {6,19,1} = totalomeOPPA;
revandomeganalyzis {7,19,1} = semomedurall;
revandomeganalyzis {8,19,1} = medianaomedurall;
revandomeganalyzis {9,19,1} = propwormsthatomeg;


%% ***************OMEGA DISTRIBUITION **********************
omedist = dataOPPA (2:sujeitosloopOPPA, 27); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes omegas

omedistvalues = [];
for i=1:sujeitosOPPA;
    iodvalues = omedist {i,1};%individual reversal distribuition values
    omedistvalues = [omedistvalues; iodvalues];    
end
 
distribuicoesomeOPPA = size (omedistvalues, 1);

if distribuicoesomeOPPA > 1 % when there is more than one subject with omegas
    omedistall = mean (omedistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraoomedistall = std (omedistvalues);
    semomedistall = desvpadraoomedistall./ (sqrt (distribuicoesomeOPPA));%in this case, what matters is the number of individuals that did the reversals
    medianaomedistall = median (omedistvalues);    
elseif  distribuicoesomeOPPA == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    omedistall = omedistvalues;
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraorevdistall./ (sqrt (1));
    medianaomedistall = revdistvalues;
elseif distribuicoesomeOPPA == 0 %if any worm makes reversals or omegas
    omedistall = zeros (1,10);
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = zeros (1,10);
end

revandomeganalyzis {2,23,1} = omedistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,23,1} = omedistall;%gives the average number of omegas at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,23,1} = desvpadraoomedistall;
revandomeganalyzis {6,23,1} = distribuicoesomeOPPA;
revandomeganalyzis {7,23,1} = semomedistall;
revandomeganalyzis {8,23,1} = medianaomedistall;

%% ****************PROPORTION OF WORMS THAT OMEGA AT A GIVEN POINT IN SPACE **********

wormomespacedist = zeros (1,10);
wormomespacedistdivision = [];
for k=1:10
    for l = 1:sujeitosOPPA
        if omedistvalues (l,k,1) ==0
           wormomespacedistdivision = [wormomespacedistdivision;0];
        elseif omedistvalues (l,k,1) ~=0
           wormomespacedistdivision = [wormomespacedistdivision;1]; 
        end
    end
        wormomesum = sum (wormomespacedistdivision);
        wormomespacedist (1,k,1) = wormomesum;
        wormomespacedistdivision = [];  
end

if sujeitosOPPA == 1 
    proportionofwormsome = wormomespacedist;
elseif sujeitosOPPA ~= 1
    proportionofwormsome =  wormomespacedist./sujeitosOPPA;
end

revandomeganalyzis {9,23,1} = proportionofwormsome;


%% Analyze for dataset PA to OP 

%******************************* REVERSALS PER SECOND **********

sujeitosPAOP = size (dataPAOP,1)-1;% because it is counting with the label row
sujeitosloopPAOP = sujeitosPAOP + 1;%because the 1st line is always the title

averagerevpersec = dataPAOP (2:sujeitosloopPAOP, 15); %extract average number of reversals a given 
%worm does in one second

averagerevpersecvalues = zeros(sujeitosPAOP,1);
for i=1:sujeitosPAOP;
    rpsvalues = averagerevpersec {i,1};
    averagerevpersecvalues(i,1) = rpsvalues;
end

if sujeitosPAOP >=3
[H, p, W] = swtest(averagerevpersecvalues, 0.05);
elseif sujeitosPAOP ==2 
    p = NaN;
elseif sujeitosPAOP ==1 
    p = NaN;
elseif sujeitosPAOP ==0
    p= NaN;
end
 
averagerevpersecall = mean (averagerevpersecvalues); %gives the avr number of reversals per second of OP to OP worms
desvpadraoavrrevpersecall = std (averagerevpersecvalues);
semavrrevpersecall = desvpadraoavrrevpersecall./ (sqrt (sujeitosPAOP));
medianaavrrevpersecall = median (averagerevpersecvalues);

revandomeganalyzis {2,4,1} = averagerevpersecvalues;
revandomeganalyzis {3,4,1} = p;
revandomeganalyzis {4,4,1} = averagerevpersecall;
revandomeganalyzis {5,4,1} = desvpadraoavrrevpersecall;
revandomeganalyzis {6,4,1} = sujeitosPAOP;
revandomeganalyzis {7,4,1} = semavrrevpersecall;
revandomeganalyzis {8,4,1} = medianaavrrevpersecall;

%% *********PROPORTION OF WORMS THAT REVERSE **********
wormsthatrevers = [];

for n = 2:sujeitosloopPAOP;
    wormrevcount = dataPAOP (n,11,1);
    wormrevcountvalues = wormrevcount {1,1};
    yesornorev = size (wormrevcountvalues);
    if yesornorev ~= 0
       wormsthatrevers = [ wormsthatrevers, 1];
    elseif yesornorev == 0
       wormsthatrevers = [ wormsthatrevers, 0];
    end
end

nwormsthatrevers = sum (wormsthatrevers); %extracts from all worms of the dataset of many of them show reversals
propwormsthatrevers = nwormsthatrevers./sujeitosPAOP;%proportion of worms in the data set that revers

%% *************REVERSALS DURATION ****************
indrevduration = dataPAOP (2:sujeitosloopPAOP, 11); %extracts duration of reversals for each animal
indrevdurationvalues = [];

if nwormsthatrevers ==0;
    indrevdurationvalues = zeros (nwormsthatrevers, 1);
elseif nwormsthatrevers ~=0;
   for j=1:sujeitosPAOP;
        irdvalues = indrevduration {j,1};
        indrevdurationvalues = [indrevdurationvalues; irdvalues];
   end
end

totalrevPAOP = size (indrevdurationvalues, 1);

if totalrevPAOP >= 3
[H, p, W] = swtest(indrevdurationvalues, 0.05);
elseif totalrevPAOP ==2 
    p = NaN;
elseif totalrevPAOP ==1 
    p = NaN;
elseif totalrevPAOP ==0
    p= NaN;
end

revdurall = mean (indrevdurationvalues);

if isnan (revdurall) == true
    indrevdurationvaluesfinal = [];
    revdurallfinal = 0;
elseif isnan (revdurall) == false
    indrevdurationvaluesfinal = indrevdurationvalues;
    revdurallfinal = revdurall;
end

desvpadraorevdurall = std (indrevdurationvaluesfinal);
semrevdurall = desvpadraorevdurall./ (sqrt (totalrevPAOP));
medianarevdurall = median (indrevdurationvaluesfinal);

revandomeganalyzis {2,8,1} = indrevdurationvaluesfinal;
revandomeganalyzis {3,8,1} = p;
revandomeganalyzis {4,8,1} = revdurallfinal;
revandomeganalyzis {5,8,1} = desvpadraorevdurall;
revandomeganalyzis {6,8,1} = totalrevPAOP;
revandomeganalyzis {7,8,1} = semrevdurall;
revandomeganalyzis {8,8,1} = medianarevdurall;
revandomeganalyzis {9,8,1} = propwormsthatrevers;


%% ***************** REVERSAL DISTRIBUITION *************
revdist = dataPAOP (2:sujeitosloopPAOP, 25); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes reversals
%zerosvector = zeros (1,11);

revdistvalues = [];
for i=1:sujeitosPAOP;
    irdvalues = revdist {i,1};%individual reversal distribuition values
    revdistvalues = [revdistvalues; irdvalues];    
end
 
distribuicoesrevPAOP = size (revdistvalues, 1);

if distribuicoesrevPAOP > 1 % when there is more than one subject with reversals
    revdistall = mean (revdistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraorevdistall = std (revdistvalues);
    semrevdistall = desvpadraorevdistall./ (sqrt (distribuicoesrevPAOP));%in this case, what matters is the number of individuals that did the reversals
    medianarevdistall = median (revdistvalues);    
elseif distribuicoesrevPAOP == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    revdistall = revdistvalues;
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = revdistvalues;
elseif distribuicoesrevPAOP == 0 %if any worm makes reversals or omegas
    revdistall = zeros (1,10);
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = zeros (1,10);
end

revandomeganalyzis {2,12,1} = revdistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,12,1} = revdistall;%gives the average number of reversals at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,12,1} = desvpadraorevdistall;
revandomeganalyzis {6,12,1} = distribuicoesrevPAOP;
revandomeganalyzis {7,12,1} = semrevdistall;
revandomeganalyzis {8,12,1} = medianarevdistall;

%% *****************PROPORTION OF WORMS THAT REVERSE AT A GIVEN POINT IN SPACE ****************

wormrevspacedist = zeros (1,10);
wormrevspacedistdivision = [];
for k=1:10
    for l = 1:sujeitosPAOP
        if revdistvalues (l,k,1) ==0
           wormrevspacedistdivision = [wormrevspacedistdivision;0];
        elseif revdistvalues (l,k,1) ~=0
           wormrevspacedistdivision = [wormrevspacedistdivision;1]; 
        end
    end
        wormsrevsum = sum (wormrevspacedistdivision);
        wormrevspacedist (1,k,1) = wormsrevsum;
        wormrevspacedistdivision = [];  
end

if sujeitosPAOP == 1 
    proportionofworms =  wormrevspacedist;
elseif sujeitosPAOP ~= 1
    proportionofworms =  wormrevspacedist./sujeitosPAOP;
end

revandomeganalyzis {9,12,1} = proportionofworms;

%% ********************* OMEGAS PER SECOND ***************************
averageomepersec = dataPAOP (2:sujeitosloopPAOP, 23); %extract the avr nume of omegas each worm does per second 

averageomepersecvalues = zeros(sujeitosPAOP,1);
for i=1:sujeitosPAOP;
    opsvalues = averageomepersec {i,1};
    averageomepersecvalues(i,1) = opsvalues;
end

if sujeitosPAOP >=3
[H, p, W] = swtest(averageomepersecvalues, 0.05);
elseif sujeitosPAOP ==2 
    p = NaN;
elseif sujeitosPAOP ==1 
    p = NaN;
elseif sujeitosPAOP ==0
    p= NaN;
end
 
averageomepersecall = mean (averageomepersecvalues);
desvpadraoavromepersecall = std (averageomepersecvalues);
semavromepersecall = desvpadraoavromepersecall./ (sqrt (sujeitosPAOP));
medianaavromepersecall = median (averageomepersecvalues);

revandomeganalyzis {2,16,1} = averageomepersecvalues;
revandomeganalyzis {3,16,1} = p;
revandomeganalyzis {4,16,1} = averageomepersecall;
revandomeganalyzis {5,16,1} = desvpadraoavromepersecall;
revandomeganalyzis {6,16,1} = sujeitosPAOP;
revandomeganalyzis {7,16,1} = semavromepersecall;
revandomeganalyzis {8,16,1} = medianaavromepersecall;

%% ****************PROPORTION OF WORMS THAT OMEGA ******************** 
wormsthatomeg = []; % off all worms, how many worms perform at least one omega turn at a given point in time

for n = 2:sujeitosloopPAOP;
    wormomecount = dataPAOP (n,19,1);
    wormomecountvalues = wormomecount {1,1};
    yesornoome = size (wormomecountvalues);
    if yesornoome ~= 0
       wormsthatomeg = [ wormsthatomeg, 1];
    elseif yesornoome == 0
       wormsthatomeg = [ wormsthatomeg, 0];
    end
end


nwormsthatomeg = sum (wormsthatomeg); %extracts from all worms of the dataset of many of them show omegas at any time point
propwormsthatomeg = nwormsthatomeg./sujeitosPAOP;%proportion of worms in the data set that do omegas

%% *************** OMEGA DURATION *****************
indomeduration = dataPAOP (2:sujeitosloopPAOP, 19); %extracts duration of reversals for each animal
indomedurationvalues = [];

if nwormsthatomeg ==0;
    indomedurationvalues = zeros (nwormsthatomeg, 1);
elseif nwormsthatomeg ~=0;
   for j=1:sujeitosPAOP;
        iodvalues = indomeduration {j,1};
        indomedurationvalues = [indomedurationvalues; iodvalues];
   end
end

totalomePAOP = size (indomedurationvalues, 1);

if totalomePAOP >= 3
[H, p, W] = swtest(indomedurationvalues, 0.05);
elseif totalomePAOP ==2
    p = NaN;    
elseif totalomePAOP ==1 
    p = NaN;
elseif totalomePAOP ==0
    p= NaN;
end

omedurall = mean (indomedurationvalues);

if isnan (omedurall) == true
    indomedurationvaluesfinal = [];
    omedurallfinal = 0;
elseif isnan (omedurall) == false
    indomedurationvaluesfinal = indomedurationvalues;
    omedurallfinal = omedurall;
end

desvpadraoomedurall = std (indomedurationvaluesfinal);
semomedurall = desvpadraoomedurall./ (sqrt (totalomePAOP));
medianaomedurall = median (indomedurationvaluesfinal);

revandomeganalyzis {2,20,1} = indomedurationvaluesfinal;
revandomeganalyzis {3,20,1} = p;
revandomeganalyzis {4,20,1} = omedurallfinal;
revandomeganalyzis {5,20,1} = desvpadraoomedurall;
revandomeganalyzis {6,20,1} = totalomePAOP;
revandomeganalyzis {7,20,1} = semomedurall;
revandomeganalyzis {8,20,1} = medianaomedurall;
revandomeganalyzis {9,20,1} = propwormsthatomeg;

%% ********************* OMEGA DISTRIBUITION **********************
omedist = dataPAOP(2:sujeitosloopPAOP, 27); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes omegas

omedistvalues = [];
for i=1:sujeitosPAOP;
    iodvalues = omedist {i,1};%individual reversal distribuition values
    omedistvalues = [omedistvalues; iodvalues];    
end
 
distribuicoesomePAOP = size (omedistvalues, 1);

if distribuicoesomePAOP > 1 % when there is more than one subject with omegas
    omedistall = mean (omedistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraoomedistall = std (omedistvalues);
    semomedistall = desvpadraoomedistall./ (sqrt (distribuicoesomePAOP));%in this case, what matters is the number of individuals that did the reversals
    medianaomedistall = median (omedistvalues);    
elseif  distribuicoesomePAOP == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    omedistall = omedistvalues;
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = omedistvalues;
elseif distribuicoesomePAOP == 0 %if any worm makes reversals or omegas
    omedistall = zeros (1,10);
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = zeros (1,10);
end

revandomeganalyzis {2,24,1} = omedistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,24,1} = omedistall;%gives the average number of omegas at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,24,1} = desvpadraoomedistall;
revandomeganalyzis {6,24,1} = distribuicoesomePAOP;
revandomeganalyzis {7,24,1} = semomedistall;
revandomeganalyzis {8,24,1} = medianaomedistall;

%% ****************PROPORTION OF WORMS THAT OMEGA AT A GIVEN POINT IN SPACE *****************

wormomespacedist = zeros (1,10);
wormomespacedistdivision = [];
for k=1:10
    for l = 1:sujeitosPAOP
        if omedistvalues (l,k,1) ==0
           wormomespacedistdivision = [wormomespacedistdivision;0];
        elseif omedistvalues (l,k,1) ~=0
           wormomespacedistdivision = [wormomespacedistdivision;1]; 
        end
    end
        wormomesum = sum (wormomespacedistdivision);
        wormomespacedist (1,k,1) = wormomesum;
        wormomespacedistdivision = [];  
end

if sujeitosPAOP == 1 
    proportionofwormsome = wormomespacedist;
elseif sujeitosPAOP ~= 1
    proportionofwormsome =  wormomespacedist./sujeitosPAOP;
end

revandomeganalyzis {9,24,1} = proportionofwormsome;


%% ****************ANALYZE FOR DATASET PA to PA ********************************** 

% *********************** REVERSALS PER SECOND ****************
sujeitosPAPA = size (dataPAPA,1)-1;% because it is counting with the label row
sujeitosloopPAPA = sujeitosPAPA + 1;%because the 1st line is always the title

averagerevpersec = dataPAPA (2:sujeitosloopPAPA, 15); %extract average number of reversals a given 
%worm does in one second

averagerevpersecvalues = zeros(sujeitosPAPA,1);
for i=1:sujeitosPAPA;
    rpsvalues = averagerevpersec {i,1};
    averagerevpersecvalues(i,1) = rpsvalues;
end

if sujeitosPAPA >3 
[H, p, W] = swtest(averagerevpersecvalues, 0.05);
elseif sujeitosPAPA ==2 
    p = NaN;   
elseif sujeitosPAPA ==1 
    p = NaN;
elseif sujeitosPAPA ==0
    p= NaN;
end

averagerevpersecall = mean (averagerevpersecvalues); %gives the avr number of reversals per second of OP to OP worms
desvpadraoavrrevpersecall = std (averagerevpersecvalues);
semavrrevpersecall = desvpadraoavrrevpersecall./ (sqrt (sujeitosPAPA));
medianaavrrevpersecall = median (averagerevpersecvalues);

revandomeganalyzis {2,5,1} = averagerevpersecvalues;
revandomeganalyzis {3,5,1} = p;
revandomeganalyzis {4,5,1} = averagerevpersecall;
revandomeganalyzis {5,5,1} = desvpadraoavrrevpersecall;
revandomeganalyzis {6,5,1} = sujeitosPAPA;
revandomeganalyzis {7,5,1} = semavrrevpersecall;
revandomeganalyzis {8,5,1} = medianaavrrevpersecall;


%% ************PROPORTION OF WORMS THAT REVERSE ***********************
wormsthatrevers = [];

for n = 2:sujeitosloopPAPA;
    wormrevcount = dataPAPA (n,10,1);
    wormrevcountvalues = wormrevcount {1,1};
    yesornorev = size (wormrevcountvalues);
    if yesornorev ~= 0
       wormsthatrevers = [ wormsthatrevers, 1];
    elseif yesornorev == 0
       wormsthatrevers = [ wormsthatrevers, 0];
    end
end

nwormsthatrevers = sum (wormsthatrevers); %extracts from all worms of the dataset of many of them show reversals
propwormsthatrevers = nwormsthatrevers./sujeitosPAPA;%proportion of worms in the data set that revers


%% ***************REVERSALS DURATION **********************
indrevduration = dataPAPA (2:sujeitosloopPAPA, 11); %extracts duration of reversals for each animal
indrevdurationvalues = [];

if nwormsthatrevers ==0;
   indrevdurationvalues = zeros (nwormsthatrevers, 1);
elseif nwormsthatrevers ~=0;
   for j=1:sujeitosPAPA;
        irdvalues = indrevduration {j,1};
        indrevdurationvalues = [indrevdurationvalues; irdvalues];
   end
end

totalrevPAPA = size (indrevdurationvalues, 1);

if totalrevPAPA >=3 
[H, p, W] = swtest(indrevdurationvalues, 0.05);
elseif totalrevPAPA ==2
    p = NaN;
elseif totalrevPAPA ==1 
    p = NaN;
elseif totalrevPAPA ==0
    p= NaN;
end
 
revdurall = mean (indrevdurationvalues);

if isnan (revdurall) == true
    indrevdurationvaluesfinal = [];
    revdurallfinal = 0;
elseif isnan (revdurall) == false
    indrevdurationvaluesfinal = indrevdurationvalues;
    revdurallfinal = revdurall;
end

desvpadraorevdurall = std (indrevdurationvaluesfinal);
semrevdurall = desvpadraorevdurall./ (sqrt (totalrevPAPA));
medianarevdurall = median (indrevdurationvaluesfinal);

revandomeganalyzis {2,9,1} = indrevdurationvaluesfinal;
revandomeganalyzis {3,9,1} = p;
revandomeganalyzis {4,9,1} = revdurallfinal;
revandomeganalyzis {5,9,1} = desvpadraorevdurall;
revandomeganalyzis {6,9,1} = totalrevPAPA;
revandomeganalyzis {7,9,1} = semrevdurall;
revandomeganalyzis {8,9,1} = medianarevdurall;
revandomeganalyzis {9,9,1} = propwormsthatrevers;


%% ********************** REVERSAL DISTRIBUITION *****************************

revdist = dataPAPA(2:sujeitosloopPAPA, 25); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes reversals

revdistvalues = [];
for i=1:sujeitosPAPA;
    irdvalues = revdist {i,1};%individual reversal distribuition values
    revdistvalues = [revdistvalues; irdvalues];    
end
 
distribuicoesrevPAPA = size (revdistvalues, 1);

if distribuicoesrevPAPA > 1 % when there is more than one subject with reversals
    revdistall = mean (revdistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraorevdistall = std (revdistvalues);
    semrevdistall = desvpadraorevdistall./ (sqrt (distribuicoesrevPAPA));%in this case, what matters is the number of individuals that did the reversals
    medianarevdistall = median (revdistvalues);    
elseif distribuicoesrevPAPA == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    revdistall = revdistvalues;
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = revdistvalues;
elseif distribuicoesrevPAPA == 0 %if any worm makes reversals or omegas
    revdistall = zeros (1,10);
    desvpadraorevdistall = zeros (1,10);
    semrevdistall = desvpadraorevdistall./ (sqrt (1));
    medianarevdistall = zeros (1,10);
end

revandomeganalyzis {2,13,1} = revdistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,13,1} = revdistall;%gives the average number of reversals at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,13,1} = desvpadraorevdistall;
revandomeganalyzis {6,13,1} = distribuicoesrevPAPA;
revandomeganalyzis {7,13,1} = semrevdistall;
revandomeganalyzis {8,13,1} = medianarevdistall;

%% **************PROPORTION OF WORMS THAT REVERSE AT A GIVEN POINT IN SPACE **************

wormrevspacedist = zeros (1,10);
wormrevspacedistdivision = [];
for k=1:10
    for l = 1:sujeitosPAPA
        if revdistvalues (l,k,1) ==0
           wormrevspacedistdivision = [wormrevspacedistdivision;0];
        elseif revdistvalues (l,k,1) ~=0
           wormrevspacedistdivision = [wormrevspacedistdivision;1]; 
        end
    end
        wormsrevsum = sum (wormrevspacedistdivision);
        wormrevspacedist (1,k,1) = wormsrevsum;
        wormrevspacedistdivision = [];  
end

if sujeitosPAPA == 1 
    proportionofworms =  wormrevspacedist;
elseif sujeitosPAPA ~= 1
    proportionofworms =  wormrevspacedist./sujeitosPAPA;
end

revandomeganalyzis {9,13,1} = proportionofworms;

%% ********************** OMEGAS PER SECOND *****************************
averageomepersec = dataPAPA (2:sujeitosloopPAPA, 23); %extract the avr nume of omegas each worm does per second 

averageomepersecvalues = zeros(sujeitosPAPA,1);
for i=1:sujeitosPAPA;
    opsvalues = averageomepersec {i,1};
    averageomepersecvalues(i,1) = opsvalues;
end

if sujeitosPAPA >= 3
[H, p, W] = swtest(averageomepersecvalues, 0.05);   
elseif sujeitosPAPA ==2 
    p = NaN;
elseif sujeitosPAPA ==1 
    p = NaN;
elseif sujeitosPAPA ==0
    p= NaN;
end
 
averageomepersecall = mean (averageomepersecvalues);
desvpadraoavromepersecall = std (averageomepersecvalues);
semavromepersecall = desvpadraoavromepersecall./ (sqrt (sujeitosPAPA));
medianaavromepersecall = median (averageomepersecvalues);

revandomeganalyzis {2,17,1} = averageomepersecvalues;
revandomeganalyzis {3,17,1} = p;
revandomeganalyzis {4,17,1} = averageomepersecall;
revandomeganalyzis {5,17,1} = desvpadraoavromepersecall;
revandomeganalyzis {6,17,1} = sujeitosPAPA;
revandomeganalyzis {7,17,1} = semavromepersecall;
revandomeganalyzis {8,17,1} = medianaavromepersecall;

%% ***************** PROPORTION OF WORMS THAT OMEGA **********************
wormsthatomeg = []; % off all worms, how many worms perform at least one omega turn at a given point in time

for n = 2:sujeitosloopPAPA;
    wormomecount = dataPAPA (n,19,1);
    wormomecountvalues = wormomecount {1,1};
    yesornoome = size (wormomecountvalues);
    if yesornoome ~= 0
       wormsthatomeg = [ wormsthatomeg, 1];
    elseif yesornoome == 0
       wormsthatomeg = [ wormsthatomeg, 0];
    end
end


nwormsthatomeg = sum (wormsthatomeg); %extracts from all worms of the dataset of many of them show omegas at any time point
propwormsthatomeg = nwormsthatomeg./sujeitosPAPA;%proportion of worms in the data set that do omegas

%% ******************** OMEGAS DURATION ***********************
indomeduration = dataPAPA (2:sujeitosloopPAPA, 19); %extracts duration of reversals for each animal
indomedurationvalues = [];

if nwormsthatomeg ==0;
    indomedurationvalues = [];
elseif nwormsthatomeg ~=0;
   for j=1:sujeitosPAPA
        iodvalues = indomeduration {j,1};
        indomedurationvalues = [indomedurationvalues; iodvalues];
   end
end

totalomePAPA = size (indomedurationvalues, 1);

if totalomePAPA >= 3
[H, p, W] = swtest(indomedurationvalues, 0.05);  
elseif totalomePAPA ==2 
    p = NaN;   
elseif totalomePAPA ==1 
    p = NaN;
elseif totalomePAPA ==0
    p= NaN;
end

omedurall = mean (indomedurationvalues);

if isnan (omedurall) == true
    indomedurationvaluesfinal = [];
    omedurallfinal = 0;
elseif isnan (omedurall) == false
    indomedurationvaluesfinal = indomedurationvalues;
    omedurallfinal = omedurall;
end

desvpadraoomedurall = std (indomedurationvaluesfinal);
semomedurall = desvpadraoomedurall./ (sqrt (totalomePAPA));
medianaomedurall = median (indomedurationvaluesfinal);

revandomeganalyzis {2,21,1} = indomedurationvaluesfinal;
revandomeganalyzis {3,21,1} = p;
revandomeganalyzis {4,21,1} = omedurallfinal;
revandomeganalyzis {5,21,1} = desvpadraoomedurall;
revandomeganalyzis {6,21,1} = totalomePAPA;
revandomeganalyzis {7,21,1} = semomedurall;
revandomeganalyzis {8,21,1} = medianaomedurall;
revandomeganalyzis {9,21,1} = propwormsthatomeg;


%% ******************* OMEGA DISTRIBUITION **********************

omedist = dataPAPA (2:sujeitosloopPAPA, 27); %extracts for each worm where in the histogram of distance
%from stimuli the worm makes omegas
%zerosvector = zeros (1,11);

omedistvalues = [];
for i=1:sujeitosPAPA;
    iodvalues = omedist {i,1};%individual reversal distribuition values
    omedistvalues = [omedistvalues; iodvalues];    
end
 
distribuicoesomePAPA = size (omedistvalues, 1);

if distribuicoesomePAPA > 1 % when there is more than one subject with omegas
    omedistall = mean (omedistvalues);% gives the mean number of reversals at a given place (but can be more than one reversal from same worm)
    desvpadraoomedistall = std (omedistvalues);
    semomedistall = desvpadraoomedistall./ (sqrt (distribuicoesomePAPA));%in this case, what matters is the number of individuals that did the reversals
    medianaomedistall = median (omedistvalues);    
elseif  distribuicoesomePAPA == 1 %if just one line, matlab does the mean between all values in the line and we don't want that
    omedistall = omedistvalues;
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = omedistvalues;
elseif distribuicoesomePAPA == 0 %if any worm makes reversals or omegas
    omedistall = zeros (1,10);
    desvpadraoomedistall = zeros (1,10);
    semomedistall = desvpadraoomedistall./ (sqrt (1));
    medianaomedistall = zeros (1,10);
end

revandomeganalyzis {2,25,1} = omedistvalues;
%sizeandspeedanalyzis {3,13,1} = p;
revandomeganalyzis {4,25,1} = omedistall;%gives the average number of omegas at a given distance from stimulus - counts both worms that do and don't do reversals
revandomeganalyzis {5,25,1} = desvpadraoomedistall;
revandomeganalyzis {6,25,1} = distribuicoesomePAPA;
revandomeganalyzis {7,25,1} = semomedistall;
revandomeganalyzis {8,25,1} = medianaomedistall;

%% ***********PROPORTION OF WORMS THAT OMEGA AT A GIVEN POINT IN SPACE *******************

wormomespacedist = zeros (1,10);
wormomespacedistdivision = [];
for k=1:10
    for l = 1:sujeitosPAPA
        if omedistvalues (l,k,1) ==0
           wormomespacedistdivision = [wormomespacedistdivision;0];
        elseif omedistvalues (l,k,1) ~=0
           wormomespacedistdivision = [wormomespacedistdivision;1]; 
        end
    end
        wormomesum = sum (wormomespacedistdivision);
        wormomespacedist (1,k,1) = wormomesum;
        wormomespacedistdivision = [];  
end

if sujeitosPAPA == 1 
    proportionofwormsome = wormomespacedist;
elseif sujeitosPAPA ~= 1
    proportionofwormsome =  wormomespacedist./sujeitosPAPA;
end

revandomeganalyzis {9,25,1} = proportionofwormsome;

%% *******************CREATE BOX PLOTS FOR REVERSALS PER SECOND ************* 
avgindrevpersecOPOP = revandomeganalyzis {2,2,1};
wormsOPOP = size (avgindrevpersecOPOP,1);

avgindrevpersecOPPA = revandomeganalyzis {2,3,1};
wormsOPPA = size (avgindrevpersecOPPA,1);

avgindrevpersecPAOP = revandomeganalyzis {2,4,1};
wormsPAOP = size (avgindrevpersecPAOP,1);

avgindrevpersecPAPA = revandomeganalyzis {2,5,1};
wormsPAPA = size (avgindrevpersecPAPA,1);

A1 = zeros (wormsOPOP,1);
 for i = 1:wormsOPOP
     A1 (i,1)= 0;
 end

A2 = zeros (wormsOPPA,1);
 for i = 1:wormsOPPA
     A2 (i,1)= 1;
 end

A3 = zeros (wormsPAOP,1);
 for i = 1:wormsPAOP
     A3 (i,1)= 2;
 end
 
A4 = zeros (wormsPAPA,1);
 for i = 1:wormsPAPA
     A4 (i,1)= 3;
 end 
 
figure
data1 = [avgindrevpersecOPOP; avgindrevpersecOPPA; avgindrevpersecPAOP;avgindrevpersecPAPA];
group1 = [ A1; A2; A3; A4];
boxplot (data1, group1,'Labels',{'F0OP_F1choiceOP','F0OP_F1choicePA', 'F0PA_F1choiceOP', 'F0PA_F1choicePA'});
%set (Labels, 'FontSize', 8);
title ('RRF3 Exp8 N2 Reversals per second')
ylabel ('Reversals/sec','FontSize',14);


%% ***********CREATE BOX PLOTS FOR REVERSALS DURATION *****************
indrevdurOPOPvalues = revandomeganalyzis {2,6,1};

indrevdurOPPAvalues = revandomeganalyzis {2,7,1};

indrevdurPAOPvalues = revandomeganalyzis{2,8,1};

indrevdurPAPAvalues = revandomeganalyzis {2,9,1};

maxreversals = [];

if totalrevOPOP > totalrevOPPA
    maxreversals = totalrevOPOP;
elseif totalrevOPOP < totalrevOPPA;
    maxreversals = totalrevOPPA;
end

if maxreversals < totalrevPAOP
    maxreversals = totalrevPAOP;
elseif maxreversals > totalrevPAOP;
    maxreversals = maxreversals;
end

if maxreversals < totalrevPAPA
    maxreversals = totalrevPAPA;
elseif maxreversals > totalrevPAPA;
    maxreversals = maxreversals;
end    
    
matrix2 = NaN (maxreversals,4);

matrix2 (1:totalrevOPOP, 1) = indrevdurOPOPvalues; %chande dimesnions
matrix2 (1:totalrevOPPA, 2) = indrevdurOPPAvalues; %change dimensions 
matrix2 (1:totalrevPAOP, 3) = indrevdurPAOPvalues; %change dimensions
matrix2 (1:totalrevPAPA, 4) = indrevdurPAPAvalues; %change dimensions

figure
boxplot (matrix2, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
title ('RRF3 Exp8 N2 Reversals duration');
ylabel ('rev dur (sec)');

%% ******Create a line graph for average number of reversals occuring at a given distance from the stimuli 
% use the average number of reversals at a given point of the distribuition
figure
for i=10:13
    plotStyle = {'m','c','g','k', 'm','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k' };
    yaxis = revandomeganalyzis (4,i,1);
    yaxisvalues = yaxis {1,1};
     
    sem = revandomeganalyzis (7,i,1);
    semvalues = sem {1,1};
    
    errorbar (spacedistr,yaxisvalues,semvalues,'Color',plotStyle {i}); 
    xlabel ('distance to stim um')
    ylabel ('avg number reversals')
    title ('RRF3 Exp8 N2')
    l2 = revandomeganalyzis (1, i,1);
    l2val = l2{1,1};
    % stores the info in a cell array that is afterwards called by legend
    legendInfo {i-9} = l2val ;
    hold on
end   

legend (legendInfo);

%% Create a line graph for average number of worms performing a reversal at a given distance from the stimuli 
figure
for i=10:13
    plotStyle = {'m','c','g','k', 'm','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k' };
    yaxis = revandomeganalyzis (9,i,1);
    yaxisvaluesprop = yaxis {1,1};
    
    plot (spacedistr, yaxisvaluesprop,plotStyle{i});
    axis ([0,25000, 0, 1]);
    xlabel ('distance to stim um')
    ylabel ('proportion of worms reversals')
    title ('RRF3 Exp8 N2')
    %l1 = 'Worm';
    l2 = revandomeganalyzis (1, i,1);
    l2val = l2{1,1};
    % stores the info in a cell array that is afterwards called by legend
    legendInfo {i-9} = l2val ;
    hold on
end   

legend (legendInfo);

%% Create box plots for omegas per second
avgindomepersecOPOPvalues = revandomeganalyzis {2,14,1};

avgindomepersecOPPAvalues = revandomeganalyzis {2,15,1};

avgindomepersecPAOPvalues = revandomeganalyzis {2,16,1};

avgindomepersecPAPAvalues = revandomeganalyzis {2,17,1};

matrix = NaN (8,4);

matrix (1:sujeitosOPOP, 1) = avgindomepersecOPOPvalues; %chande dimesnions
matrix (1:sujeitosOPPA, 2) = avgindomepersecOPPAvalues; %change dimensions 
matrix (1:sujeitosPAOP, 3) = avgindomepersecPAOPvalues; %change dimensions
matrix (1:sujeitosPAPA, 4) = avgindomepersecPAPAvalues; %change dimensions

figure
boxplot (matrix, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
title ('RRF3 Exp8 N2 Omegas per second');
ylabel ('ome/sec');


%% Create box plots for omegas duration
indomedurOPOPvalues = revandomeganalyzis {2,18,1};

indomedurOPPAvalues = revandomeganalyzis {2,19,1};

indomedurPAOPvalues = revandomeganalyzis {2,20,1};

indomedurPAPAvalues = revandomeganalyzis {2,21,1};

maxomegas = [];

if totalomeOPOP > totalomeOPPA
    maxomegas = totalomeOPOP;
elseif totalomeOPOP < totalomeOPPA;
    maxomegas = totalomeOPPA;
end

if maxomegas < totalomePAOP
    maxomegas = totalomePAOP;
elseif maxomegas > totalomePAOP;
    maxomegas = maxomegas;
end

if maxomegas < totalomePAPA
    maxomegas = totalomePAPA;
elseif maxomegas > totalomePAPA;
    maxomegas = maxomegas;
end    
    
matrix2 = NaN (maxomegas,4);

matrix2 (1:totalomeOPOP, 1) = indomedurOPOPvalues; %chande dimesnions
matrix2 (1:totalomeOPPA, 2) = indomedurOPPAvalues; %change dimensions 
matrix2 (1:totalomePAOP, 3) = indomedurPAOPvalues; %change dimensions
matrix2 (1:totalomePAPA, 4) = indomedurPAPAvalues; %change dimensions

figure
boxplot (matrix2, 'Labels',{'OP to OP','OP to PA', 'PA to OP', 'PA to PA'});
%ylim (-1,1);
title ('RRF3 Exp8 N2 omega duration');
ylabel ('ome dur (sec)');

%% Create a line graph for average number of omegas occuring at a given distance from the stimuli 
figure
for i=22:25
    plotStyle = {'m','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k','m','m','c','g','k','m','c','g','k'};
    yaxiso = revandomeganalyzis (4,i,1);
    yaxisovalues = yaxiso {1,1};
    
    semo = revandomeganalyzis (7,i,1);
    semovalues = semo {1,1};
    
    errorbar (spacedistr,yaxisovalues,semovalues, plotStyle{i}); 
    xlabel ('distance to stim um')
    ylabel ('avg number omegas')
    title ('RRF3 Exp8 N2 all')
    %l1 = 'Worm';
    l2 = revandomeganalyzis (1, i,1);
    l2val = l2{1,1};
    % stores the info in a cell array that is afterwards called by legend
    legendInfo {i-21} = l2val ;
    hold on
end   

legend (legendInfo);

%% Create a line graph for average number of worms performing an omega at a given distance from the stimuli 
figure
for i=22:25
    plotStyle = {'m','c','g','k', 'm','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k', 'm','c','g','k','m','c','g','k','m','c','g','k','m','c','g','k', };
    yaxis = revandomeganalyzis (9,i,1);
    yaxisvaluesprop = yaxis {1,1};
    
    plot (spacedistr, yaxisvaluesprop,plotStyle{i});  
    axis ([0,25000, 0, 1]);
    xlabel ('distance to stim um')
    ylabel ('proportion of worms omegas')
    title ('RRF3 Exp8 N2 all')
    %l1 = 'Worm';
    l2 = revandomeganalyzis (1, i,1);
    l2val = l2{1,1};
    % stores the info in a cell array that is afterwards called by legend
    legendInfo {i-21} = l2val ;
    hold on
end   

legend (legendInfo);


RRF3_Exp30_ZC2834_all_reversals_and_omegas_F1 = revandomeganalyzis;
